﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{

}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="dictype-form" style="padding: 15px 0 0 0;">
            <input type="hidden" name="Id" value="@Model.Id" />
            <input type="hidden" id="ParentId" name="ParentId" value="@Model.ParentId" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">父级菜单</label>
                <div class="layui-input-block">
                    <ul id="menuTree" class="dtree" data-id="0"></ul>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="menuname" lay-verify="required|menuname" placeholder="一般不要超过10个汉字"
                           autocomplete="off" class="layui-input" value="@Model.MenuName" maxlength="20" />

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">代码</label>
                <div class="layui-input-block">
                    <input type="text" name="menucode" lay-verify="required" placeholder="功能唯一标识"
                           autocomplete="off" class="layui-input @(!string.IsNullOrEmpty(Model.MenuCode)?"readonly-b":"")" value="@Model.MenuCode" @(Model.Id > 0 ? "readonly" : "") />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">访问路径:</label>
                <div class="layui-input-block">
                    <input type="text" name="menuurl" placeholder="菜单访问路径"
                           autocomplete="off" class="layui-input" value="@Model.MenuUrl" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标样式</label>
                <div class="layui-input-block">

                    <input type="text" name="menuicon" required="" placeholder="可以填写css样式"
                           autocomplete="off" class="layui-input" value="@Model.MenuIcon">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">排序</label>
                <div class="layui-input-block">
                    <input type="number" name="menuseq" placeholder="一般用于调整显示顺序"
                           autocomplete="off" class="layui-input" value="@Model.MenuSeq" min="0" max="99" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">是否菜单</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="Isvisible" lay-skin="switch" @(Model.Isvisible ? "checked" : "")>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">是否启用</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="Isenable" lay-skin="switch" @(Model.Isenable ? "checked" : "")>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">打开方式</label>
                <div class="layui-input-block">
                    <input type="radio" name="targettype" value="0" title="默认框架" @(Model.TargetType == 0 ? "checked" : "") />
                    <input type="radio" name="targettype" value="1" title="新页面" @(Model.TargetType == 1 ? "checked" : "") />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">拥有功能</label>
                <div class="layui-input-block">
                    @if (ViewBag.FunList != null)
                    {
                        foreach (var item in ViewBag.FunList)
                        {
                            bool exists = false;
                            if (ViewBag.MenuFunList != null)
                            {
                                foreach (var fun in ViewBag.MenuFunList)
                                {
                                    if (fun.FunCode == item.FunCode)
                                    {
                                        exists = true;
                                        break;
                                    }
                                }
                            }
                            <input name="ckfuncode" lay-skin="primary" type="checkbox" value="@item.FunCode" title="@item.FunName" @(exists ? "checked" : "")>
                        }

                    }
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">备注:</label>
                <div class="layui-input-block">
                    <textarea name="remark" placeholder="请输入备注" class="layui-textarea">@Model.Remark</textarea>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-hide" lay-submit lay-filter="menu-submit" id="menu-submit">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>


@section scripts{

    <script type="text/javascript">
        layui.use(['lscom', 'form', 'dtree'], function () {
            var $ = layui.$;
            var lscom = layui.lscom;
            var form = layui.form;
            var dtree = layui.dtree;

            var index = parent.layer.getFrameIndex(window.name);
            //parent.layer.iframeAuto(index);

            dtree.render({
                elem: "#menuTree",
                initLevel: "1",
                method: 'get',
                url: "/Systems/Menu/GetdtreeData?id=@Model.Id",
                icon: ["0", "-1"],
                response: { message: "msg", statusCode: 0 },
                dataStyle: "layuiStyle",  //使用layui风格的数据格式
                dataFormat: "list",  //配置data的风格为list
                select: true,
                selectInitVal: "@Model.ParentId", 
                selectTips:'请选择父级菜单'
            });

            // 绑定节点点击
            dtree.on("node('menuTree')", function (obj) {
                $("#ParentId").val(obj.param.nodeId);
            });
            form.render();
            window.submitForm = function () {
                $("#menu-submit").click();
            }

            form.on('submit(menu-submit)', function (data) {

                data.field.Isvisible = data.field.Isvisible == "on";
                data.field.Isenable = data.field.Isenable == "on";
                let funcode = [];
                $('input[name="ckfuncode"]').each(function () {
                    if ($(this).prop('checked')) {
                        funcode.push($(this).val());
                    }
                });
                data.field.funcode = funcode.join(',');
                var field = data.field;

                var waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: "/Systems/Menu/Save",
                    data: JSON.stringify(field),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        data.index = index;
                        let parentWindow = parent.selectedWindow().window;
                        parentWindow.saveCallback(data);
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            });
        });
    </script>

}

